package it.antonio.ilpizze.swing.event;

import it.antonio.ilpizze.reflection.MethodAccessor;

import java.util.HashMap;
import java.util.Map;

public class EventMap {
	
	private Map<String, EventEntry> eventmap = new HashMap<String, EventEntry>();
	
	public void subscribe(MethodAccessor method, Object component){
		eventmap.put(method.getName(), new EventEntry(method, component));
	}
	
	public void throwEvent(String event, Object... events){
		EventEntry entry = eventmap.get(event);
		if(entry != null){
			entry.throwEvent(events);
		}
	}
	
	public class EventEntry {
		private MethodAccessor method;
		private Object instance;

		public EventEntry(MethodAccessor method, Object instance) {
			super();
			this.method = method;
			this.instance = instance;
		}

		public void throwEvent(Object... events) {
			method.invoke(instance, events);
		}
	}
}
